﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using NeuralStock.Entities;

namespace NeuralStock.Tools
{
    public class CsvReader<T>
    {
        public ICsvLineParser ImportedClassInstance { get; set; }

        public CsvReader(ICsvLineParser importedClassInstance)
        {
            this.ImportedClassInstance = importedClassInstance;
        }

        public IEnumerable<T> GetDataFromCsv(string filePath, bool firstLineWithHeaders = false)
        {
            List<T> returnList = new List<T>();

            using (var reader = new StreamReader(filePath))
            {
                string line;

                if (firstLineWithHeaders)
                {
                    reader.ReadLine(); // in order to remove headers
                }

                while ((line = reader.ReadLine()) != null)
                {
                    returnList.Add((T)ImportedClassInstance.ParseLine(line));
                }
            }
            return returnList;
        }
    }
}
